'''
    Shuihu-Cyg
'''

import scrapy
import time
import json
import requests
from lxml import etree

# from selenium import webdriver

# start_url = 'http://cyg.changyou.com/list#!gameId=16&sort=offlineTime,asc&goodsType=1&page=2&job=4'

class CYGSpider(scrapy.Spider):
    name = 'cyg-fs'
    # start_urls = [start_url]
    headers = {'Host': 'www.jycinema.com',
            'Content-Type': 'application/x-www-form-urlencoded;charset=UTF-8',
            'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/52.0.2743.116 Safari/537.36'}

    custom_settings = {
        'FEED_EXPORT_ENCODING' : 'utf-8'
    }

    def listRequest(self, page):
        url = 'http://cyg.changyou.com/list/filterSortGoods.json'
        data = {"filterList":[{"field":"goodsType","value":"1","type":1},{"field":"job","value":"4","type":1},{"field":"gameId","value":"16"}],"gameServer":{"gameId":"16"},"sort":{"key":"offlineTime","value":"asc"},"pageNumber":page,"pageSize":20}
        return scrapy.Request(url, method='POST', body=json.dumps(data), headers={'Content-Type':'application/json'})

    def get_detail_url(self, goods_code):
        return 'http://public.npt.changyou.com//details/16/'+ goods_code +'.html'

    def get_jsl(self,goods_code):
        r = requests.get(url=self.get_detail_url(goods_code))

        if r.status_code == 200:
            selector = etree.HTML(r.text)
            jsl = selector.xpath('//*[@class="m-intro-block"]/div/div[2]/div[2]/p[2]/span[2]')[0].text
            print('fetched... ' + str(goods_code) + ', value:' + jsl)
            return jsl
        else:
            print(str(goods_code) + 'load error!' + str(r.status_code))
            return 0

    def start_requests(self):
        return [self.listRequest(1)]

    # def __init__(self):
    #     # use any browser you wish
    #     chromeOptions = webdriver.ChromeOptions()
    #     prefs = {"profile.managed_default_content_settings.images":2}
    #     chromeOptions.add_experimental_option("prefs",prefs)
    #     self.browser = webdriver.Chrome('./chromedriver',chrome_options=chromeOptions)
    
    # def parseItem(self, url):
    #     print('parse item ' + url)
    #     self.browser.get(url)
    #     time.sleep(2)
    #     title = self.browser.find_element_by_class_name('goodstitle').text
    #     price = self.browser.find_element_by_xpath('//*[@class="m-money-points"]').text
    #     jingshen = self.browser.find_element_by_xpath('//*[@class="m-intro-block"]/div/div[2]/div[2]/p[2]/span[2]').text
    #     yield {'title': title ,'price': price, '精神力': jingshen}

    def parse(self, response):
        response_data = json.loads(response.text)['data']
        items = response_data['list']
        for item in items:
            itemData = item['goods']
            goodsCode = itemData['goodsCode']
            jsl = self.get_jsl(goodsCode)

            export_data = {'goodsCode': goodsCode,
            'title': itemData['title'],
            'goodsName': itemData['goodsName'], 
            'price': itemData['price'],
            'jsl' : jsl,
            'url': 'http://cyg.changyou.com/details/?goodsCode=' + goodsCode,
            'attrs': itemData['attrs']}

            yield export_data
        
        
        nextPageNum = response_data['nextPage']
        print('--- next page: '+ str(nextPageNum))
        if nextPageNum > 0:
            yield self.listRequest(nextPageNum)

        # scrapy.shell.inspect_response(response, self)

        #start browser
        # self.browser.get(start_url)
        # print('--- open url...')

        # # #loading time interval
        # time.sleep(3)

        # print('--- sleep 3 sec...')

        # for item in self.browser.find_elements_by_xpath('//*[@id="goodsListWrap"]/ul/li'):
        #     item_link_href = item.find_element_by_xpath('div/div[1]/h2/a').get_property('href')
        #     print('--- '+ item_link_href)

        #     time.sleep(3)
        #     self.browser.get(item_link_href)
        #     time.sleep(2)
        #     title = self.browser.find_element_by_class_name('goodstitle').text
        #     price = self.browser.find_element_by_xpath('//*[@class="m-money-points"]').text
        #     jingshen = self.browser.find_element_by_xpath('//*[@class="m-intro-block"]/div/div[2]/div[2]/p[2]/span[2]').text
        #     yield {'title': title ,'price': price, '精神力': jingshen}
            
        #     # time.sleep(3)
        #     # yield scrapy.Request(item_link_href, callback=self.parseItem)
        #     # yield {'title': title,'price': price}

        print('--- finished...')
        # next_page = response.css('div.prev-post > a ::attr(href)').extract_first()
        # if next_page:
        #     yield scrapy.Request(response.urljoin(next_page), callback=self.parse)



    # def __del__(self):
    #     self.browser.close()